﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ras2ClassLib
{
    public class SongController
    {
        public IEnumerable<UISong> GetAllSongs()
        {
            Ras2DBDataContext db = new Ras2DBDataContext();

            var result = from song in db.Songs
                         join performer in db.Performers
                            on song.PerformerID equals performer.ID
                         orderby song.Name ascending
                         select new UISong
                         {
                             ID = song.ID,
                             Name = song.Name,
                             PerformerName = performer.Name,
                             PerformerID = performer.ID,
                             MP3Path = song.MP3Path,
                             Description = song.Description
                         };

            return result;
        }

        /// <summary>
        /// Sækja öll lög á Song formi.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Song> GetAllSongs2()
        {
            Ras2DBDataContext db = new Ras2DBDataContext();

            var result = from song in db.Songs
                         select song;

            return result;
        }

        public IEnumerable<UISong> GetAllSongsWithPerformer(int nID)
        {
            Ras2DBDataContext db = new Ras2DBDataContext();

            var result = from song in db.Songs
                         where song.PerformerID == nID
                         orderby song.Name ascending
                         select new UISong
                         {
                             ID = song.ID,
                             Name = song.Name
                         };

            return result;
        }

        public void UpdateSong(UISong s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            Song original = (from a in context.Songs
                                where a.ID == s.ID
                                select a).SingleOrDefault();
            if (original != null)
            {
                original.Name = s.Name;
                original.PerformerID = s.PerformerID;
                original.MP3Path = s.MP3Path;
                original.Description = s.Description;
                context.SubmitChanges();
            }
        }

        public void AddSong(UISong s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            Song ss = new Song();
            ss.Name        = s.Name;
            ss.PerformerID = s.PerformerID;
            ss.MP3Path     = s.MP3Path;
            ss.Description = s.Description;
            context.Songs.InsertOnSubmit(ss);
            context.SubmitChanges();
        }

        public void DeleteSong(UISong s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            Song ss = new Song();
            ss.Name = s.Name;
            ss.PerformerID = s.PerformerID;
            ss.MP3Path = s.MP3Path;
            ss.Description = s.Description;
            context.Songs.Attach(ss);
            context.Songs.DeleteOnSubmit(ss);
            context.SubmitChanges();
        }		
    }
}
